from HTMLTestRunner import HTMLTestRunner
from random import randint
from time import sleep
from selenium import webdriver
import unittest
from selenium.webdriver.support.select import Select


class TestJXC(unittest.TestCase):
    driver = webdriver.Firefox()

    @classmethod
    def setUpClass(cls):
        cls.driver.implicitly_wait(2)
        cls.driver.get('http://localhost/jxc/index.asp')

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def setUp(self):
        sleep(1)

    def tearDown(self):
        sleep(1)

    # 测试登录成功
    def test_login_ok(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.assertIn('后台管理系统', self.driver.page_source)

    # 测试删除单位
    def test_del_danwei(self):
        # 点击系统设置
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        sys_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(sys_css).click()
        sleep(1)
        # 点击计量单位管理
        jldw_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(jldw_css).click()
        sleep(1)
        # 进入右框架
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        # 点击全选
        allchoose_xpath = '//*[@id="chkall"]'
        self.driver.find_element_by_xpath(allchoose_xpath).click()
        sleep(1)
        # 点击删除
        del_danwei_css = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(del_danwei_css).click()
        sleep(1)
        # 进入提示框,并确定
        self.driver.switch_to.alert.accept()
        sleep(1)

    # 测试新增单位
    def test_add_danwei(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        # 点击添加
        add_danwei_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(add_danwei_css).click()
        sleep(1)
        # 添加单位
        self.driver.find_element_by_name('danwei').send_keys('条')
        sleep(1)
        # 点击确定
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 进入提示框，并确定
        self.driver.switch_to.alert.accept()
        sleep(1)

    # 测试添加大类
    def test_add_bigclass(self):
        # 进入右框架
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_css_selector(
            '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)').click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        # 点击添加大类1
        add_big_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(add_big_css).click()
        sleep(1)
        # 输入类名
        self.driver.find_element_by_name('bigclass').send_keys('aaeqw2aaa')
        sleep(1)
        # 点击提交
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 进入提示框，并确定
        self.driver.switch_to.alert.accept()
        sleep(3)
        # 点击添加大类2
        add_big_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(add_big_css).click()
        sleep(3)
        # 输入类名
        self.driver.find_element_by_name('bigclass').clear()
        self.driver.find_element_by_name('bigclass').send_keys('baqw2bbb')
        sleep(1)
        # 点击提交
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 进入提示框，并确定
        self.driver.switch_to.alert.accept()
        sleep(1)
        # 点击添加大类3
        add_big_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(add_big_css).click()
        sleep(1)
        # 输入类名
        self.driver.find_element_by_name('bigclass').clear()
        self.driver.find_element_by_name('bigclass').send_keys('Adaqw2dd')
        sleep(1)
        # 点击提交
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 进入提示框，并确定
        self.driver.switch_to.alert.accept()
        sleep(1)

    # 测试添加小类
    def test_add_samllclass(self):
        # 进入左框架
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        # 点击产品小类管理
        smell_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(smell_css).click()
        sleep(1)

        # 进入右框架
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        # 点击添加小类
        add_smell_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(add_smell_css).click()
        sleep(1)
        # 选择大类
        sel_lst = Select(self.driver.find_element_by_name('bigclass'))
        lst_count = len(sel_lst.options)
        rand = randint(0, lst_count - 1)
        sel_lst.select_by_index(rand)
        sleep(1)
        # 输入类
        self.driver.find_element_by_name('smallclass').send_keys('1111211')
        sleep(1)
        # 点击定
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        # 进入提示框，并点击确定
        self.driver.switch_to.alert.accept()
        sleep(1)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(TestJXC('test_login_ok'))
    suite.addTest(TestJXC('test_del_danwei'))
    suite.addTest(TestJXC('test_add_danwei'))
    suite.addTest(TestJXC('test_add_bigclass'))
    suite.addTest(TestJXC('test_add_samllclass'))
    return suite


with open('c:/jyc.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='测试报告')
    runner.run(test_suite())
